//
// Nibbler.cpp for Nibbler in /home/florian/nibbler
// 
// Made by Florian Coulon
// Login   <coulon_a@epitech.net>
// 
// Started on  Thu Mar  1 01:48:41 2012 Florian Coulon
// Last update Thu Mar  1 01:48:43 2012 Florian Coulon
//

#include    <string>
#include    <cstdlib>
#include    <iostream>
#include    <sstream>

#include "Nibbler.hh"
#include "utils.hh"

<qq<q

// Static annexe

template <typename X>
static X   string_to_X(std::string value)
{
    X   res;
    std::istringstream  _to(value);
    _to >> res;
    if (!_to.good())
        return (res);
    return (0);
}

//  Constructeur & destructeur

Nibbler::Nibbler(char **av)
{
    std::string     param[4];

    for (x = 0, y = 1; y < 4; ++x, ++y)
        param[x].assign(av[y]);
    this->x = string_to_X<int>(param[0]);
    this->y = string_to_X<int>(param[1]);
    if (this->x < 9 || this->y < 9) {
        print_usage();
        exit(EXIT_FAILURE); }
    this->map = new Objects*[this->y + 1];
    for (int i = 0; i <= this->y; ++i)
        this->map[i] = new Objects[this->x + 1];
    this->gen_map();
}

Nibbler::~Nibbler()
{
    for ( int i = 0; i <= this->y; ++i)
        delete [](this->map[i]);
    delete [] map;
}

// Complexe methode

bool    Nibbler::move_down()
{
    int     i = 0, j;
    Objects       **tmp;

    tmp = new Objects*[this->y + 1];
    for (int i = 0; i <= this->y; ++i)
        tmp[i] = new Objects[this->x + 1];
    for (j = 0; j <= this->y; ++j)
        for (i = 0; i <= this->x; ++i)
            tmp[j][i] = this->map[j][i];
    for (j = 1; j < this->y; ++j)
    {
        for (i = 1; i < this->x; ++i)
            if (this->map[j][i].get_Type() == HEAD)
                break;
        if (this->map[j][i].get_Type() == HEAD)
            break;
    }



    std::cout << "map[j][i] = " << map[j][i].get_Type() << " And head = " << HEAD << std::endl;

    if (this->map[j][i].get_Type() == BTM_WALL ||
            this->map[j][i].get_Type() == BODY ||
            this->map[j][i].get_Type() == TAIL)
        return false;


}

void    Nibbler::move_left()
{

}

void    Nibbler::move_up()
{

}

void    Nibbler::move_right()
{

}

void    Nibbler::put_in_paused()
{

}

void    Nibbler::quit_game()
{

}

void    Nibbler::speed_running()
{

}

bool    Nibbler::key_event(t_key _key)
{
    switch (_key)
    {
    case (UP):
        this->move_up();
    case (LEFT):
        this->move_left();
    case (RIGHT):
        this->move_right();
    case (DOWN):
        this->move_down();
    case (QUIT):
        this->quit_game();
        return false;
    case (PAUSE):
        this->put_in_paused();
        return false;
    case (SPACE):
        this->speed_running();
    default:
        return true;
    }
}

void    Nibbler::aff_map()
{
    for (int j = 0; j <= this->y; ++j) {
        for (int i = 0; i <= this->x; ++i)
            std::cout << this->map[j][i].get_Type() << "\t";
        std::cout << std::endl; }
}

void    Nibbler::gen_map()
{
    this->map[0][0].set_Type(L_TOP_CORNER);
    this->map[0][this->x].set_Type(R_TOP_CORNER);
    this->map[this->y][0].set_Type(L_BTM_CORNER);
    this->map[this->y][this->x].set_Type(R_BTM_CORNER);
    for (int i = 1; i < this->x; ++i)
        this->map[0][i].set_Type(TOP_WALL);
    for (int i = 1; i < this->y; ++i)
        this->map[i][0].set_Type(L_WALL);
    for (int i = 1; i < this->x; ++i)
        this->map[0][i].set_Type(TOP_WALL);
    for (int i = 1; i < this->x; ++i)
        this->map[this->y][i].set_Type(BTM_WALL);
    for (int i = 1; i < this->y; ++i)
        this->map[i][this->x].set_Type(R_WALL);
    for (int j = 1; j < this->y; ++j)
        for (int i = 1; i < this->x; ++i)
            this->map[j][i].set_Type(GROUND);
    map[this->y / 2 + 1][this->x - 5].set_Type(HEAD);
    map[this->y / 2 + 1][this->x - 4].set_Type(BODY);
    map[this->y / 2 + 1][this->x - 3].set_Type(BODY);
    map[this->y / 2 + 1][this->x - 2].set_Type(TAIL);
}

void    Nibbler::exec() { }
